#pragma once
#include "windows.h"
#include "ThreadImpl.h"

namespace NamedPipe
{
	const DWORD BUFSIZE			= 4096;
	const DWORD PIPE_TIMEOUT	= 5000;
	const char PIPE_NAME[]		= "wang";

	class CServer: public ThreadImpl<CServer>
	{
	public:
		CServer(void);
		~CServer(void);
		void Start(const char *lpzPipeName);
		void Stop();
		int Send(HANDLE hPipe, LPCVOID pData, DWORD nLen);

		virtual void OnConnect(HANDLE hPipe){};
		virtual void OnClose(HANDLE hPipe){};
		virtual void OnReceive(HANDLE hPipe, LPCVOID pData, DWORD nLen){};
		virtual void OnStop(){};

	protected:
		void Accept(LPVOID);
		void InstanceThread(void *data);

		bool m_bStop;
		HANDLE m_hThread;
	};

	class CClient
	{
	public:
		CClient(void);
		~CClient(void);
		HANDLE Connect(const char *lpzPipeName);
		void Close(HANDLE hPipe);
		int Send(HANDLE hPipe, LPCVOID pData, DWORD nLen);
		int Recv(HANDLE hPipe, LPVOID pData, DWORD nLen);
	};
}